package com.cyz.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyz.dao.CoursedesignDao;
import com.cyz.dao.StudentDao;
import com.cyz.dao.TeacherDao;
import com.cyz.dao.UserDao;
import com.cyz.entity.CoursedesignEntity;
import com.cyz.entity.StudentEntity;
import com.cyz.entity.TeacherEntity;
import com.cyz.entity.UserEntity;
import com.cyz.service.StudentService;
import com.cyz.vo.StudentDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 *
 *
 * @author chenyizhen
 * @email
 * @date 2023-12-26 14:18:30
 */
@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    StudentDao studentDao;

    @Autowired
    UserDao userDao;

    @Autowired
    CoursedesignDao coursedesignDao;

    @RequestMapping("/queryName")
    private List<String> queryName(){
        List<StudentEntity> studentEntities = studentDao.selectList(new QueryWrapper<>());
        return studentEntities.stream().map(item -> item.getName()).collect(Collectors.toList());
    }

    @PostMapping("/courseManage")
    private List<StudentDTO> courseManage(){
        List<StudentDTO> studentDTOS = studentDao.courseManage();
        return studentDTOS;
    }

    @PostMapping("/queryCourse/{account}")
    private String queryCourse(@PathVariable String account){
        CoursedesignEntity coursedesignEntity = getCoursedesignEntity(account);
        return coursedesignEntity == null ? "" : coursedesignEntity.getDesignName();
    }

    private CoursedesignEntity getCoursedesignEntity(String account) {
        UserEntity entity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("account", account));

        StudentEntity studentEntity = studentDao.selectOne(new QueryWrapper<StudentEntity>().eq("name", entity.getName()));

        int id = studentEntity.getId();

        CoursedesignEntity coursedesignEntity = coursedesignDao.selectOne(new QueryWrapper<CoursedesignEntity>().eq("student_id", id));

        return coursedesignEntity;
    }

    @PostMapping("updateCourse/{account}")
    private String updateCourse(@PathVariable String account, String task){
        CoursedesignEntity entity = coursedesignDao.selectOne(new QueryWrapper<CoursedesignEntity>().eq("design_name", task));

        if (entity == null){
            return "课程不存在";
        }

        CoursedesignEntity coursedesignEntity = getCoursedesignEntity(account);

        if (coursedesignEntity == null){
            coursedesignEntity = entity;

            UserEntity user = userDao.selectOne(new QueryWrapper<UserEntity>().eq("account", account));
            String name = user.getName();
            StudentEntity studentEntity = studentDao.selectOne(new QueryWrapper<StudentEntity>().eq("name", name));

            coursedesignEntity.setDesignName(task);
            coursedesignEntity.setStudentId(studentEntity.getId());
        }else {
            UserEntity user = userDao.selectOne(new QueryWrapper<UserEntity>().eq("account", account));
            String name = user.getName();
            StudentEntity studentEntity = studentDao.selectOne(new QueryWrapper<StudentEntity>().eq("name", name));

            coursedesignEntity.setDesignName(task);
            coursedesignEntity.setStudentId(studentEntity.getId());
        }


        int i = coursedesignDao.updateById(coursedesignEntity);

        return "课程已更新";

    }
    /**
     * 列表
     */
//    @RequestMapping("/list")
//    //@RequiresPermissions("user:student:list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = studentService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }
//
//
//    /**
//     * 信息
//     */
//    @RequestMapping("/info/{id}")
//    public R info(@PathVariable("id") Integer id){
//		StudentEntity student = studentService.getById(id);
//
//        return R.ok().put("student", student);
//    }
//
//    /**
//     * 保存
//     */
//    @RequestMapping("/save")
//    public R save(@RequestBody StudentEntity student){
//		studentService.save(student);
//
//        return R.ok();
//    }
//
//    /**
//     * 修改
//     */
//    @RequestMapping("/update")
//    public R update(@RequestBody StudentEntity student){
//		studentService.updateById(student);
//
//        return R.ok();
//    }
//
//    /**
//     * 删除
//     */
//    @RequestMapping("/delete")
//    public R delete(@RequestBody Integer[] ids){
//		studentService.removeByIds(Arrays.asList(ids));
//
//        return R.ok();
//    }

}
